package com.qst.controller;

import com.qst.BaseServlet;
import com.qst.ExamException;
import com.qst.RequestUtil;
import com.qst.entity.AssessmentType;
import com.qst.entity.Schedule;
import com.qst.entity.Team;
import com.qst.entity.User;
import com.qst.service.AssessmentService;
import com.qst.service.ScheduleService;
import com.qst.service.TeamService;
import com.qst.service.impl.ServiceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/schedule")
@SuppressWarnings("all")
public class ScheduleController extends BaseServlet {

    /**
     * teamServiceSsmImpl 未完成
     */
     @Autowired
     @Qualifier("teamServiceImpl")
     TeamService teamService;
     @Autowired
     @Qualifier("assessmentServiceSsmImpl")
     private AssessmentService assessmentService;
     @Autowired
     @Qualifier("scheduleServiceSsmImpl")
     private ScheduleService scheduleService;

    @RequestMapping("/list.action")
    public String list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = getCurrentUser(req);
        List<Schedule> scheduleList = scheduleService.findByCreator(user);
        req.setAttribute("scheduleList", scheduleList);
//        WebUtil.forward(req, resp, "/schedule/list.jsp");
        return "/schedule/list";
    }

    @RequestMapping("/create.action")
    public String create(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = getCurrentUser(req);
        Schedule schedule = (Schedule) req.getAttribute("schedule");
        if (schedule == null) {
            schedule = new Schedule();
            Date today = new Date(System.currentTimeMillis());
            SimpleDateFormat stf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            schedule.setBeginDate(stf.format(today));
            schedule.setEndDate(stf.format(today));
            req.setAttribute("schedule", schedule);
        }
        List<Team> teamList = teamService.findByCreator(user.getId());
        req.setAttribute("teamList", teamList);
        List<AssessmentType> assessmentList = assessmentService.findAllAssessment();
        req.setAttribute("assessmentList", assessmentList);
//        WebUtil.forward(req, resp, "/schedule/create.jsp");
        return "/schedule/create";
    }


    @RequestMapping("/delete.action")
    public String delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = RequestUtil.getInt(req, "id");
        try {
            scheduleService.deleteSchedule(id);
        } catch (ExamException ex) {
            ex.printStackTrace();
            addError(req, ex.getMessage());
        }
//        WebUtil.redirect(req, resp, "/schedule/list.action");
        return "redirect:/schedule/list.action";
    }

    @RequestMapping("/edit.action")
    public String edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = getCurrentUser(req);
        Integer id = RequestUtil.getInt(req, "id");
        Schedule schedule = (Schedule) req.getAttribute("schedule");
        if (schedule == null) {
            schedule = scheduleService.findById(id);
            req.setAttribute("schedule", schedule);
        }
        List<Team> teamList = teamService.findByCreator(user.getId());
        req.setAttribute("teamList", teamList);
        List<AssessmentType> assessmentList = assessmentService.findAllAssessment();
        req.setAttribute("assessmentList", assessmentList);
//        WebUtil.forward(req, resp, "/schedule/edit.jsp");
        return "/schedule/edit";
    }


    @RequestMapping("/save.action")
    public String save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Schedule schedule = new Schedule();
        try {
            User user = getCurrentUser(req);
//            schedule.setId(RequestUtil.getInt(req, "id"));
            schedule.setBeginDate(RequestUtil.getString(req, "beginDate"));
            schedule.setEndDate(RequestUtil.getString(req, "endDate"));
            schedule.setDuration(RequestUtil.getInt(req, "duration"));
            schedule.setCreateDate(RequestUtil.getDate(req, "createDate"));
            schedule.setStatus(1);
            schedule.setAssessmentId(RequestUtil.getInt(req, "assessmentId"));
            schedule.setTeamId(RequestUtil.getInt(req, "teamId"));
            schedule.setQuestionNumber(RequestUtil.getInt(req, "questionNumber"));
            schedule.setCreatorId(user.getId());
            scheduleService.saveSchedule(schedule);
//            WebUtil.redirect(req, resp, "/schedule/view.action?id="+schedule.getId());
            return  "redirect:/schedule/view.action?id="+schedule.getId();
        } catch (ExamException ex) {
            ex.printStackTrace();
            addError(req, ex.getMessage());
            req.setAttribute("schedule", schedule);
//            WebUtil.forward(req, resp, "/schedule/create.action");
            return "redirect:/schedule/create.action";
        }
    }


    @RequestMapping("/update.action")
    public String update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Schedule schedule = new Schedule();
        try {
            User user = getCurrentUser(req);
            schedule.setId(RequestUtil.getInt(req, "id"));
            schedule.setBeginDate(RequestUtil.getString(req, "beginDate"));
            schedule.setEndDate(RequestUtil.getString(req, "endDate"));
            schedule.setDuration(RequestUtil.getInt(req, "duration"));
//            schedule.setCreateDate(RequestUtil.getDate(req, "createDate"));
            schedule.setStatus(RequestUtil.getInt(req, "status"));
            schedule.setAssessmentId(RequestUtil.getInt(req, "assessmentId"));
            schedule.setTeamId(RequestUtil.getInt(req, "teamId"));
            schedule.setQuestionNumber(RequestUtil.getInt(req, "questionNumber"));
            schedule.setCreatorId(user.getId());
            scheduleService.updateSchedule(schedule);
//            WebUtil.redirect(req, resp, "/schedule/view.action?id=" + schedule.getId());
            return "redirect:/schedule/view.action?id=" + schedule.getId();
        } catch (ExamException ex) {
            ex.printStackTrace();
            addError(req, ex.getMessage());
            req.setAttribute("schedule", schedule);
//            WebUtil.forward(req, resp, "/schedule/update.action");
            return "chedule/update.action";
        }
    }

    @RequestMapping("/view.action")
    public String view(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = RequestUtil.getInt(req, "id");
        Schedule schedule = scheduleService.findById(id);
        req.setAttribute("schedule", schedule);
//        WebUtil.forward(req, resp, "/schedule/view.jsp");
        return "schedule/view";

    }
}
